<?php

// 1.1 创建server对象监听端口
$server = new Swoole\WebSocket\Server('0.0.0.0', 9501);
// 1.2 设置server配置
$server->set([
    'enable_static_handler' => true,    // 开启静态文件请求处理功能
    'document_root'         => '/app/swoole-study/data', // v4.4.0以下版本, 此处必须为绝对路径
    'log_file'              => '/app/swoole-study/log/swoole.log',
    //wss 代码部分
//    'ssl_key_file' => '/etc/nginx/***.key',
//    'ssl_cert_file' => '/etc/nginx/***.pem'
]);

// 2.1 监听连接事件
$server->on('open', function (\Swoole\WebSocket\Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});

// 2.2 监听消息接收事件
$server->on('message', function (\Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server");
});

// 2.3 监听关闭事件
$server->on('close', function (\Swoole\WebSocket\Server $server, $fd) {
    echo "client {$fd} closed\n";
});

// 3 启动服务
$server->start();

// https://blog.csdn.net/xiao199306/article/details/82621140
// https://blog.csdn.net/flyingdog123/article/details/89353948